// pages/navigation/old/competition/competition.js
let db=''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollLeft: 0,
    focusPage: 1,
    focusTotalPage: 1,
    multiplePage: 1,
    multipleTotalPage: 1,
    noticePage: 1,
    noticeTotalPage: 1,
     //最新的一条
     newest:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.cloud.database().collection('competiton_info')
    .orderBy("date","desc")
    .get()
    .then(res=>{
      console.log('获得全部竞赛信息成功',res);
      var list=res.data
      for(var i=0;i<list.length;i++)
      list[i].date=this.getDateAndTime(list[i].date)
      this.setData({
        focusList:list,
        newest:list[0]
      })
    })
    .catch(res=>{
      console.log('获取全部竞赛信息失败',res);
    })
    
    
  },
   //根据输入的关键字进行搜索
   onSearch(e){
    console.log('输出搜索内容',e);
    db = wx.cloud.database()
    const _=db.command
    db.collection('competiton_info')
    .where({ 
      title:db.RegExp({ 
      regexp:e.detail, 
      option:'i' 
    }) 
  }) 
      .orderBy("date","desc")
      .get()
      .then(res=>{
       console.log('成功输出',res);
       var list=res.data
       for(var i=0;i<list.length;i++)
       list[i].date=this.getDateAndTime(list[i].date)
       this.setData({
         focusList:list
       })
      })
      .catch(res=>{
        console.log('输出失败',res);
      })
   },
   //跳转详情页
   getContent(optional){
    console.log('输出看看',optional)
    wx.navigateTo({
        url: '/pages/navigation/old/competition/detail/detail?id=' + optional.currentTarget.dataset.id,
      })
   
   }, 
  getDateAndTime:function(date) {
    var Y= date.getFullYear();
    var M = date.getMonth() + 1;
    M = M < 10 ? '0' + M : M;
    var D = date.getDate();
    D = D < 10 ? '0' + D : D;
    var hour = date.getHours();
    hour = hour < 10? '0' + hour : hour;
    var minute = date.getMinutes();
    minute = minute < 10? '0' + minute : minute;
    return Y + '年' + M + '月' + D + '日' + ' ' + hour + ':' + minute; 
  }
})